package com.yanqu.road.dao.impl.monopolymarket;

import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.monopolymarket.config.MonopolyMarketBackEndConfig;
import com.yanqu.road.entity.monopolymarket.config.MonopolyMarketNpcBloodConfig;
import com.yanqu.road.entity.monopolymarket.config.MonopolyMarketResExtendParam;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description
 * @Author cwq
 * @Data 2021/8/12 15:59
 */
public class MonopolyMarketBackEndConfigDaoImpl extends TempDao {

    public MonopolyMarketBackEndConfig getMonopolyMarketBackEndConfig(int channelId) {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_s_monopolymarket_backend_config where `channelId` = ?";
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            call.setInt(i++, channelId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parseData(rs);
            }
            rs = call.executeQuery();
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    protected MonopolyMarketBackEndConfig parseData(ResultSet rs) throws SQLException {
        MonopolyMarketBackEndConfig monopolyMarketBackEndConfig = new MonopolyMarketBackEndConfig();
        monopolyMarketBackEndConfig.setChannelId(rs.getInt("channelId"));
        monopolyMarketBackEndConfig.setMapSize(rs.getInt("mapSize"));
        monopolyMarketBackEndConfig.setMapBase(rs.getInt("mapBase"));
        monopolyMarketBackEndConfig.setMapAdd(rs.getInt("mapAdd"));
        monopolyMarketBackEndConfig.setMapRound(rs.getInt("mapRound"));
        monopolyMarketBackEndConfig.setMapMidValue(rs.getInt("mapMidValue"));
        monopolyMarketBackEndConfig.setResConfig(rs.getString("resConfig"));
        monopolyMarketBackEndConfig.setBloodVolume(rs.getString("bloodVolume"));

        // 解析extendParam
        monopolyMarketBackEndConfig.setResExtendParamMap(parseExtendParam(monopolyMarketBackEndConfig.getResConfig()));
        monopolyMarketBackEndConfig.setNpcBloodConfigList(parseNpcBloodConfig(monopolyMarketBackEndConfig.getBloodVolume()));

        return monopolyMarketBackEndConfig;
    }

    public static Map<Integer, List<MonopolyMarketResExtendParam>> parseExtendParam(String resConfig) {
        Map<Integer, List<MonopolyMarketResExtendParam>> map = new HashMap<>();
        // 解析extendParam
        JSONObject jsonObject = JSONObject.parseObject(resConfig);
        jsonObject.forEach((k, v) -> {
            List<MonopolyMarketResExtendParam> list = new ArrayList<>();
            String[] extendParamArray = ((String) v).split("\\|");
            for (String item : extendParamArray) {
                MonopolyMarketResExtendParam resExtendParam = new MonopolyMarketResExtendParam();
                String[] paramArray = item.split(";");
                resExtendParam.setServerNum(Integer.parseInt(paramArray[0]));
                resExtendParam.setNum(Integer.parseInt(paramArray[1]));
                resExtendParam.setCoefficient(Integer.parseInt(paramArray[2]));
                list.add(resExtendParam);
            }
            map.put(Integer.valueOf(k), list);
        });
        return map;
    }

    public static List<MonopolyMarketNpcBloodConfig> parseNpcBloodConfig(String bloodVolume) {
        List<MonopolyMarketNpcBloodConfig> npcBloodConfigList = new ArrayList<>();
        // 0=2001:102110;850000|102120;850000|102130;850000|102140;850000|102150;850000|102210;850000#...
        String[] npcStrings = bloodVolume.split("&");
        for (String npcString : npcStrings) {
            String[] npcInfos = npcString.split("=");
            if (npcInfos.length != 2) {
                // 配置错误
                logger.info("榷场 bloodVolume 配置错误！bloodVolume：{}，npcString：{}", bloodVolume, npcString);
                continue;
            }
            Integer day = Integer.valueOf(npcInfos[0]);
            String[] npcBloodStrings = npcInfos[1].split("#");
            MonopolyMarketNpcBloodConfig npcBloodConfig = new MonopolyMarketNpcBloodConfig();
            for (String npcBloodStr : npcBloodStrings) {
                String[] npcBloodInfo = npcBloodStr.split(":");
                if (npcBloodInfo.length != 2) {
                    // 配置错误
                    logger.error("榷场 bloodVolume 配置错误！bloodVolume：{}，npcBloodStr：{}", bloodVolume, npcBloodStr);
                    throw new RuntimeException("榷场 bloodVolume 配置错误！");
                }
                Integer id = Integer.valueOf(npcBloodInfo[0]);
                String bloodStr = npcBloodInfo[1];
                npcBloodConfig.getNpcObjectStrMap().put(id, bloodStr);
            }
            npcBloodConfig.setDay(day);
            npcBloodConfigList.add(npcBloodConfig);
        }
        return npcBloodConfigList;
    }

}
